'use strict';
const Service = require('egg').Service;
class ShipperService extends Service {
  async addShipper(data) {
    const res = await this.app.mysql.insert('shipper', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
  // 更新
  async updateShipper(data) {
    const res = await this.app.mysql.update('shipper', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
  async getShipperById(id) {
    return await this.app.mysql.get('shipper', { id });
  }
  // 获取快递列表
  async getShipperList(data) {
    const page = data.page;
    const name = data.name;
    const pageSize = data.pageSize;
    const offset = (page - 1) * pageSize;
    // 模糊查询
    // const res = await this.app.mysql.select('shipper', {
    //   where: { name },
    //   offset,
    //   limit: pageSize,
    // });
    const res = await this.app.mysql.query(`select * from shipper where name like '%${name}%' limit ${pageSize} offset ${offset}`);
    const totalRes = await this.app.mysql.query(`select * from shipper where name like '%${name}%'`);
    // 查询快递总条数
    return {
      page,
      pageSize,
      total: totalRes.length,
      data: res,
    };
  }

  // 根据查询条件获取快递列表
  async getShipperListByQuery(data) {
    const res = await this.app.mysql.select('shipper', {
      where: data,
    });
    return res;
  }
  // 删除某个快递
  async deleteShipper(data) {
    const res = await this.app.mysql.delete('shipper', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }

  // 获取快递模板列表
  async getFreightTemplateList(data) {
    const res = await this.app.mysql.select('freight_template', {
      where: data,
    });
    return res;
  }
  // 获取寄件人信息
  async getSenderData() {
    const res = await this.app.mysql.select('settings');
    return res;
  }

  // 保存寄件人
  async saveSenderData(data) {
    // 更新
    let res = {};
    if (data.id) {
      res = await this.app.mysql.update('settings', data);
    } else {
      // 保存
      res = await this.app.mysql.insert('settings', data);
    }
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }

  // 获取偏远地区

  async getExceptarea() {
    const res = await this.app.mysql.select('except_area', { where: {} });
    if (res.length) {
      for (const item of res) {
        if (item.area) {
          const areaIds = item.area.split(',');
          // console.log('areaIds:', areaIds);
          const sqlStr = `select * from region where id in (${areaIds.join(',')})`;
          // const sqlStr = 'select * from region where id in (\'6\',\'8\',\'9\',\'21\')';
          // console.log('sqlStr:', sqlStr);
          const areaNameArr = await this.app.mysql.query(sqlStr);
          let areaNameStr = '';
          areaNameArr.forEach(item => {
            areaNameStr += item.name + ',';
          });
          areaNameStr = areaNameStr.slice(0, -1);
          // console.log('areaNameStr:', areaNameStr);
          item.areaName = areaNameStr;
        }
      }
    }
    return res;
  }

  // 获取所有的省份
  async getAllProvice() {
    const region = await this.app.mysql.select('region', { where: { parent_id: 1 } });
    return region;
  }
  // 保存偏远地区
  async saveExceptArea(data) {

    console.log('saveExceptArea data:', data);
    const res = await this.app.mysql.insert('except_area', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
  // 通过id获取偏远地区
  async getExceptAreaById(id) {
    // console.log('getExceptAreaById id:', id);
    const res = await this.app.mysql.get('except_area', { id });
    // console.log('res:', res);
    if (res.area) {
      const areaIds = res.area.split(',');
      // console.log('areaIds:', areaIds);
      const sqlStr = `select * from region where id in (${areaIds.join(',')})`;
      // const sqlStr = 'select * from region where id in (\'6\',\'8\',\'9\',\'21\')';
      // console.log('sqlStr:', sqlStr);
      const areaNameArr = await this.app.mysql.query(sqlStr);
      let areaNameStr = '';
      areaNameArr.forEach(item => {
        areaNameStr += item.name + ',';
      });
      areaNameStr = areaNameStr.slice(0, -1);
      // console.log('areaNameStr:', areaNameStr);
      res.areaName = areaNameStr;
    }
    if (res) {
      return {
        code: 0,
        data: res,
      };
    }
    return {
      code: -1,
    };
  }
  async updateExceptArea(data) {
    const res = await this.app.mysql.update('except_area', data);
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
  async deleteExceptArea(id) {
    const res = await this.app.mysql.delete('except_area', { id });
    if (res.affectedRows === 1) {
      return {
        code: 0,
      };
    }
    return {
      code: -1,
    };
  }
}

module.exports = ShipperService;
